package com.joolun.web.controller.cms;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.joolun.common.annotation.Log;
import com.joolun.common.core.controller.BaseController;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.common.enums.BusinessType;
import com.joolun.mall.domain.ArticleCategory;
import com.joolun.mall.service.IArticleCategoryService;
import com.joolun.common.utils.poi.ExcelUtil;
import com.joolun.common.core.page.TableDataInfo;

/**
 * 文章分类Controller
 * 
 * @author www.joolun.com
 * @date 2021-10-18
 */
@RestController
@RequestMapping("/mall/articleCategory")
public class ArticleCategoryController extends BaseController {
    @Autowired
    private IArticleCategoryService articleCategoryService;

    /**
     * 查询文章分类列表
     */
    @PreAuthorize("@ss.hasPermi('mall:articleCategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArticleCategory articleCategory) {
        startPage();
        List<ArticleCategory> list = articleCategoryService.selectArticleCategoryList(articleCategory);
        return getDataTable(list);
    }

    /**
     * 导出文章分类列表
     */
    @PreAuthorize("@ss.hasPermi('mall:articleCategory:export')")
    @Log(title = "文章分类", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ArticleCategory articleCategory) {
        List<ArticleCategory> list = articleCategoryService.selectArticleCategoryList(articleCategory);
        ExcelUtil<ArticleCategory> util = new ExcelUtil<ArticleCategory>(ArticleCategory.class);
        return util.exportExcel(list, "articleCategory");
    }

    /**
     * 获取文章分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:articleCategory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(articleCategoryService.selectArticleCategoryById(id));
    }

    /**
     * 新增文章分类
     */
    @PreAuthorize("@ss.hasPermi('mall:articleCategory:add')")
    @Log(title = "文章分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArticleCategory articleCategory) {
        return toAjax(articleCategoryService.insertArticleCategory(articleCategory));
    }

    /**
     * 修改文章分类
     */
    @PreAuthorize("@ss.hasPermi('mall:articleCategory:edit')")
    @Log(title = "文章分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArticleCategory articleCategory) {
        return toAjax(articleCategoryService.updateArticleCategory(articleCategory));
    }

    /**
     * 删除文章分类
     */
    @PreAuthorize("@ss.hasPermi('mall:articleCategory:remove')")
    @Log(title = "文章分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {

//        Long[] notIds = articleCategoryService.getNotUseIds(ids);


        // if (articleCategoryService.getNotUseIds(ids)) {
        //     return AjaxResult.error("该分类存在文章,不允许删除");
        // }
        return toAjax(articleCategoryService.deleteArticleCategoryByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('mall:articleCategory:remove')")
    @Log(title = "文章分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id) {



         if (articleCategoryService.checkUsedArticleCategoryById(id)) {
             return AjaxResult.error("该分类存在文章,不允许删除");
         }
        return toAjax(articleCategoryService.deleteArticleCategoryById(id));
    }
}
